﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Web;
using System.Text;
using System.IO;
namespace SPBusiness.ExtensionMethods
{
    public static class StringExtension
    {
       public static string EncodeUrlPath(this string decodedString)
       {
          return HttpUtility.UrlPathEncode(decodedString);          
       }
       public static string DecodeUrlPath(this string encodedString)
       {
          return HttpUtility.UrlDecode(encodedString);
       }

       public static string EncodeHtml(this string decodedString)
       {
          return HttpUtility.HtmlEncode(decodedString);
       }
       public static string DecodeHtml(this string encodedString)
       {
          return HttpUtility.HtmlDecode(encodedString);
       }

       public static string SanitizePath(this string inputString)
       {          
          try
          {             
             string regexSearch = new string(Path.GetInvalidPathChars());
             Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
             return r.Replace(inputString, string.Empty);            
          }
          catch (Exception ex)
          {
             Logging.LogMessase(typeof(StringExtension).Namespace, typeof(StringExtension).Name, "SanitizePath", ex);
             throw;
          }
       }


        public static string PrepareToUrl(this string inputString)
        {
            try
            {
                inputString = Regex.Replace(inputString, @"\s+", "");

                StringBuilder sb = new StringBuilder();
                var charsArray = inputString.Normalize(NormalizationForm.FormD).ToCharArray();

                foreach (char letter in charsArray)
                {
                    if (CharUnicodeInfo.GetUnicodeCategory(letter) != UnicodeCategory.NonSpacingMark)
                    {
                        sb.Append(letter);
                    }
                }

                return sb.ToString();
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(StringExtension).Namespace, typeof(StringExtension).Name, "PrepareToUrl", ex);
                throw;
            }

        }

        public static bool IsGuid(this string strId)
        {
            if (string.IsNullOrEmpty(strId))
            {
                return false;
            }
            strId = strId.Trim();
            if (strId.Length < 0x20)
            {
                return false;
            }
            if (strId.Contains("x") || strId.Contains("X"))
            {
                strId = strId.Replace(" ", "");
                return Regex.IsMatch(strId, @"^\{0[x|X][a-fA-F\d]{8},(0[x|X][a-fA-F\d]{4},){2}\{(0[x|X][a-fA-F\d]{2},){7}0[x|X][a-fA-F\d]{2}\}\}$", RegexOptions.Compiled);
            }
            return Regex.IsMatch(strId, @"^([a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}|\([a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\)|\{[a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\}|[a-fA-F\d]{32})$", RegexOptions.Compiled);
        }
    }
}
